package cn.jingyuan.swan.system.domain.sys.dao.mysql;

import cn.jingyuan.swan.cloud.dao.DaoException;
import cn.jingyuan.swan.cloud.dao.mybatisplus.AbstractMyBatisPlusCrudDao;
import cn.jingyuan.swan.system.domain.sys.dao.ApiDao;
import cn.jingyuan.swan.system.domain.sys.entity.Api;
import cn.jingyuan.swan.system.domain.sys.mapper.ApiMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class ApiDaoMySql extends AbstractMyBatisPlusCrudDao<Api, Long> implements ApiDao {

    @Resource
    ApiMapper mapper;

    @Override
    public Api getByQueryWrapper(LambdaQueryWrapper<Api> wrapper) {
        try {
            return mapper.selectOne(wrapper);
        } catch (Exception e) {
            throw new DaoException(e);
        }
    }

    @Override
    public Api getByApiCode(String apiCode) {
        LambdaQueryWrapper<Api> wrapper = this.queryWrapper().eq(Api::getApiCode, apiCode);

        return this.getByQueryWrapper(wrapper);
    }

    @Override
    public List<Api> listByQueryWrapper(LambdaQueryWrapper<Api> wrapper) {
        return mapper.selectList(wrapper);
    }

    @Override
    public List<Api> listByServiceId(String serviceId) {
        LambdaQueryWrapper<Api> wrapper = this.queryWrapper().eq(Api::getServiceId, serviceId);
        return mapper.selectList(wrapper);
    }

    @Override
    public int countByQueryWrapper(LambdaQueryWrapper<Api> wrapper) {
        return mapper.selectCount(wrapper);
    }

    @Override
    public int countByApiCode(String apiCode) {
        LambdaQueryWrapper<Api> wrapper = this.queryWrapper().eq(Api::getApiCode, apiCode);

        return mapper.selectCount(wrapper);
    }

    @Override
    public BaseMapper<Api> getMapper() {
        return this.mapper;
    }

}
